#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWindow>
#include <QScreen>
#include <QPainter>
#include <QFile>
#include <QTextStream>
#include <QContextMenuEvent>

#include "FramelessHelper/FramelessHelper.h"
#include "systempreferencesdialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent, Qt::FramelessWindowHint),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadQssFile();

    auto helper = new FramelessHelper(this);
    helper->setDraggableMargins(3, 3, 3, 3);
    helper->setMaximizedMargins(3, 3, 3, 3);
    helper->setTitleBarHeight(32);

    ui->left_widget->setProperty("form","leftMenuBar");

    helper->addExcludeItem(ui->minimizeButton);
    helper->addExcludeItem(ui->maximizeButton);
    helper->addExcludeItem(ui->closeButton);

    connect(ui->minimizeButton, &QPushButton::clicked,
            helper, &FramelessHelper::triggerMinimizeButtonAction);
    connect(ui->maximizeButton, &QPushButton::clicked,
            helper, &FramelessHelper::triggerMaximizeButtonAction);
    connect(ui->closeButton, &QPushButton::clicked,
            helper, &FramelessHelper::triggerCloseButtonAction);

    connect(helper, &FramelessHelper::maximizedChanged,
            this, &MainWindow::updateMaximizeButton);

    ui->maximizeButton->setIcon(QIcon(QStringLiteral(":/res/maximize-button1.png")));

    // 为 pushButton_more 安装事件过滤器
    ui->pushButton_more->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::loadQssFile()
{
    QFile file(":/style.qss");
    bool isOk = file.open(QIODevice::ReadOnly);
    if(isOk)
    {
        QTextStream text(&file);
        QString styleSheet=text.readAll();
        this->setStyleSheet(styleSheet);
        file.close();
    }
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->pushButton_more && event->type() == QEvent::ContextMenu) {
        // 创建一级菜单
        QMenu *mainMenu = new QMenu(this);

        // 创建二级菜单
        QMenu *secondLevelMenu1 = new QMenu("二级菜单1", mainMenu);
        QMenu *secondLevelMenu2 = new QMenu("二级菜单2", mainMenu);

        // 创建三级菜单
        QMenu *thirdLevelMenu1 = new QMenu("三级菜单1", secondLevelMenu1);
        QAction *action1 = new QAction("三级菜单项1", thirdLevelMenu1);
        thirdLevelMenu1->addAction(action1);

        QAction *action2 = new QAction("二级菜单项1", secondLevelMenu1);
        secondLevelMenu1->addMenu(thirdLevelMenu1);
        secondLevelMenu1->addAction(action2);

        QAction *action3 = new QAction("二级菜单项2", secondLevelMenu2);
        secondLevelMenu2->addAction(action3);

        mainMenu->addMenu(secondLevelMenu1);
        mainMenu->addMenu(secondLevelMenu2);

        // 显示菜单
        QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent*>(event);
        mainMenu->exec(ui->pushButton_more->mapToGlobal(contextMenuEvent->pos()));

        return true;
    }
    return QMainWindow::eventFilter(obj, event);
}

void MainWindow::updateMaximizeButton(bool maximized)
{
    if (maximized) {
        ui->maximizeButton->setIcon(QIcon(QStringLiteral(":/res/maximize-button2.png")));
        ui->maximizeButton->setToolTip(tr("Restore"));
    } else {
        ui->maximizeButton->setIcon(QIcon(QStringLiteral(":/res/maximize-button1.png")));
        ui->maximizeButton->setToolTip(tr("Maximize"));
    }
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

//    QPainter painter(this);
//    QImage backgroundImage(QStringLiteral(":/res/background.png"));
//    painter.drawImage(contentsRect(), backgroundImage);


//    painter.setPen(Qt::red);
//    painter.drawRect(rect().adjusted(0, 0, -1, -1));
//    painter.setPen(Qt::blue);
//    painter.drawRect(rect().adjusted(4, 4, -5, -5));

}

void MainWindow::syncPosition()
{
    QWindow *window = windowHandle();
    QScreen *screen = window->screen();

    window->setX(screen->availableGeometry().width() / 2 + 10);
}

void MainWindow::on_pushButton_systemPerferences_clicked()
{
    SystemPreferencesDialog temp;
    temp.exec();
}

void MainWindow::on_pushButton_calibrationCorrection_clicked(bool checked)
{
    ui->widget_mainForm->SetCalibrationCorrection(checked);
}
